#pragma once

#include "StaticObject.h"
#include <iostream>
#include <list>
using namespace std;

#define brickNormalWIDTH 45
#define brickNormalHEIGHT 45

struct BrickFragment
{
	D3DXVECTOR2 _Position;
	DWORD _Time;
	float _Vx;
	float _Vy;
};

class BrickNormal : public StaticObject
{
private:
	Sprite *_normalSprite;
	Sprite *_explodeSprite;

	list<BrickFragment*>* _Fragments; // danh sach cac mieng gach be
	RECT _Rect;
	float _Vx;
	float _Vy;
	float _g;
	float _Vx2; // cai lop vien gach dau vien gach goi 	
	float _Vy2;
	D3DXVECTOR2 _DrawPosition;
	DWORD _Time;
	bool _isFirstTime;
	bool _add;
	void Add(D3DXVECTOR2 Position)
	{
		float halfSize = 45 / 4;

		//top left
		BrickFragment* fragment = new BrickFragment();
		fragment->_Position = Position;
		fragment->_Position.x += halfSize;
		fragment->_Vx = _Vx;
		fragment->_Vy = _Vy;
		fragment->_Time = timeGetTime();
		_Fragments->push_back(fragment);

		// top right
		fragment = new BrickFragment();
		fragment->_Position = Position;
		fragment->_Position.x += halfSize;
		fragment->_Position.y += halfSize;
		fragment->_Vx = _Vx2;
		fragment->_Vy = _Vy2;
		fragment->_Time = timeGetTime();
		_Fragments->push_back(fragment);

		//bot left
		fragment = new BrickFragment();
		fragment->_Position = Position;
		fragment->_Vx = -_Vx;
		fragment->_Vy = _Vy;
		fragment->_Time = timeGetTime();
		_Fragments->push_back(fragment);

		//bot right
		fragment = new BrickFragment();
		fragment->_Position = Position;
		fragment->_Position.y += halfSize;
		fragment->_Vx = -_Vx2;
		fragment->_Vy = _Vy2;
		fragment->_Time = timeGetTime();
		_Fragments->push_back(fragment);
	}
public:
	BrickNormal(int id, D3DXVECTOR2 position, int MovingWidth): StaticObject(id, position, MovingWidth)
	{
		_w = brickNormalWIDTH;
		_h = brickNormalHEIGHT;
		_type = BrickNormalType;
		_movingWidth = MovingWidth;
		_add = false;
		_isFirstTime=false;

		this->_Rect.left = 0;
		this->_Rect.right = 25;
		this->_Rect.top = 0;
		this->_Rect.bottom = 25;

		this->_Fragments = new list<BrickFragment*>();
		this->_Vx = 500;
		this->_Vy = 200;
		this->_g = -30;

		this->_Vx2 = 500;
		this->_Vy2 = 700;
	}
	~BrickNormal()
	{ 
		delete _normalSprite;
		delete _explodeSprite;
	}
	void ProcessBrick()
	{

		if(!_add) 
		{
			Add(GetPosition());
			_add = true;
		}
	}
	void Update(int Delta)
	{
		if(_state==hurt)
		{
			ProcessBrick();

			list<BrickFragment*> ::iterator i, end;
			i = _Fragments -> begin(); 
			//end == _Fragments->end();

			while(i != _Fragments->end())
			{
				BrickFragment* fragment = (*i);
				_Time = timeGetTime() - fragment->_Time;

				if(_Time >= 1000)
				{
					i = _Fragments->erase(i);
				}
				else
				{
					fragment->_Position.y -= fragment->_Vy * Delta / 1000 - _g * _Time / 1000;
					fragment->_Position.x += fragment->_Vx * Delta / 1000;
					i++; 
				}
			} 
		}
	}

	void LoadResources(LPD3DXSPRITE SpriteHandler, Texture *texture)
	{
		_normalSprite = new Sprite(SpriteHandler, texture, brickNormalWIDTH, brickNormalHEIGHT, 1, 0);
		_explodeSprite = new Sprite(SpriteHandler, texture, 22.5f, 22.5f, 3, 2);
	}
	void RenderFrame(LPDIRECT3DSURFACE9 BackBuffer, ViewPort *viewport)
	{
		if(_state==normal)
		{
			_normalSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, 1, 1, viewport);
		}
		else if(_state==hurt)
		{
			list<BrickFragment*> ::iterator i, end;
			i = _Fragments->begin();

			while(i != _Fragments->end())
			{
				_DrawPosition = (*i)->_Position;
				_explodeSprite -> Render(BackBuffer, (int) _DrawPosition.x, (int) _DrawPosition.y, 255, 1, 1,viewport);

				i++;
			}
		}
	}
};